package com.ruisoft.common;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.lang.reflect.Method;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.json.JSONObject;

/**
 * 通用公共方法类
 * @author lenovo
 *
 */
public class CommonUtil {

    private static final Logger LOG = Logger.getLogger(CommonUtil.class);
    public static final Pattern PAT_PARAM = Pattern.compile("^(?:[^=&]+=[^=&]*&)*(?:[^=&]+=[^=&]*)$");

    /**
     * 获得一个UUID
     * @return String UUID
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    public static String get32UUID() {
        return getUUID().replaceAll("-", "");
    }

    public static String getPKUUID() {
        return getCurDateSortStr().concat("-").concat(getUUID().replaceAll("-", "").substring(0,16));

    }

    /**
     *
     * @return
     */
    public static String getCurDateTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
    /**
     *
     * @return
     */
    public static String getCurDate() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    /**
     *
     * @return
     */
    public static String getCurDateSortStr() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }
    /**
     * <p>
     * 输出POJO类中的所有属性及其值
     * </p>
     * 输出格式<code>[attr=value,...]</code>
     * <p>
     * POJO类属性值通过对应的<code>getter</code>方法获取， boolean型值也可以通过形如<code>isXXX</code>
     * 的方法获取
     * </p>
     *
     * @param pojo
     *            POJO类对象
     *
     * @return 格式化后的属性值字符串
     */
    public static <T> String pojoString(T pojo) {
        if (pojo == null)
            return "";

        String mName = null;
        StringBuffer str = new StringBuffer("[");
        Object[] params = null;

        for (Method md : pojo.getClass().getMethods()) {
            mName = md.getName();
            if (mName.startsWith("get") || mName.startsWith("is")) {
                if (mName.equals("getClass"))
                    continue;
                if (md.getParameterTypes().length > 0)
                    continue;

                try {
                    str.append(mName.substring(3, 4).toLowerCase().concat(mName.substring(4))).append('=').append(md.invoke(pojo, params));
                } catch (Exception e) {
                    LOG.info(e);
                } finally {
                    str.append(',');
                }
            }
        }
        if (str.length() > 1) {
            str.deleteCharAt(str.length() - 1);
        }
        str.append(']');

        return str.toString();
    }



    public static JSONObject getJsonData(String info){
        LOG.debug("请求参数：" + info);
        if (PAT_PARAM.matcher(info).matches()) {
            String[] param = info.split("&");
            String k = null, v = null;
            JSONObject json = new JSONObject();
            for (String p : param) {
                try {
                    k = p.split("=")[0];
                    v = p.split("=")[1];
                } catch (ArrayIndexOutOfBoundsException e) {
                    v = "";
                } finally {
                    json.put(k, v);
                }
            }
            return json;
        }
        return new JSONObject(info);
    }
}
